﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace zTalk
{
    class Emoticon
    {
        static Emoticon[] Emoticons = {
            new Emoticon(":)", ":smile:"),
            new Emoticon(":-)", ":smile:"),
            new Emoticon(";)", ":smile:"),
            new Emoticon(";-)", ":smile:"),
            new Emoticon(":(", ":sad:"),
            new Emoticon(":-(", ":sad:"),
            new Emoticon(":D", ":laugh:"),
            new Emoticon(":-D", ":laugh:"),
            new Emoticon(":P", ":tounge:"),
            new Emoticon(":-P", ":tounge:"),
        };

        static Dictionary<string, string> Originals;

        static Emoticon()
        {
            Originals = new Dictionary<string, string>();
            Originals.Add(":smile:", "smile.png");
            Originals.Add(":sad:", "sad.png");
            Originals.Add(":laugh:", "laugh.png");
            Originals.Add(":tounge:", "tounge.png");
        }

        private string shortName;
        private string originalName;

        public Emoticon(string shortName, string originalName)
        {
            this.shortName = shortName;
            this.originalName = originalName;
        }

        public static List<object> Parse(string message)
        {
            List<object> items = new List<object>();

            foreach (Emoticon e in Emoticons)
            {
                message = message.Replace(e.shortName, e.originalName);
            }

            int start = 0;
            int end = 0;
            while (message != "")
            {
                start = message.IndexOf(':');

                if (start != -1)
                {
                    end = message.IndexOf(':', start + 1);
                    if (end != -1)
                    {
                        items.Add(new Run(message.Substring(0, start)));

                        string sm = message.Substring(start, end - start + 1);
                        if (Originals.ContainsKey(sm))
                        {
                            Image img = new Image();
                            string uri = "pack://application:,,,/zTalk;component/resources/";
                            ImageSource src = new BitmapImage(new Uri(uri + Originals[sm]));
                            img.Source = src;
                            img.Width = src.Width;
                            img.Height = src.Height;
                            items.Add(img);

                            message = message.Substring(end + 1);
                        }
                        else
                        {
                            items.Add(new Run(sm));
                            message = message.Substring(end + 1);
                        }
                    }
                    else
                    {
                        items.Add(new Run(message.Substring(0, start + 1)));
                        message = message.Substring(start + 1);
                    }
                }
                else
                {
                    items.Add(new Run(message));
                    break;
                }
            }

            return items;
        }
    }
}
